package com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.entity.TBarinUser;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.dto.XjrBaseUserListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;

import java.util.List;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.mapper.TBarinUserMapper;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.ITBarinUserService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 用户信息表 服务实现类
 *
 * @author dongxinhang
 * @since 2024-04-26
 */
@Service
@AllArgsConstructor
public class TBarinUserServiceImpl extends BaseService<TBarinUserMapper, TBarinUser> implements ITBarinUserService {


	@Override
	public IPage<TBarinUser> getPageList(XjrBaseUserListDto pageListDto) {
		Wrapper<TBarinUser> wrapper = Wrappers.<TBarinUser>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getF_Account()), TBarinUser::getFAccount, pageListDto.getF_Account())
				.like(!StringUtil.isEmpty(pageListDto.getF_Password()), TBarinUser::getFPassword, pageListDto.getF_Password())
				.like(!StringUtil.isEmpty(pageListDto.getF_Telephone()), TBarinUser::getFTelephone, pageListDto.getF_Telephone())
				.like(!StringUtil.isEmpty(pageListDto.getF_FirmName()), TBarinUser::getFFirmname, pageListDto.getF_FirmName())
				.like(!StringUtil.isEmpty(pageListDto.getF_CreditCode()), TBarinUser::getFCreditcode, pageListDto.getF_CreditCode())
				.eq(!StringUtil.isEmpty(pageListDto.getF_PlaceOfRegistration()), TBarinUser::getFPlaceofregistration, pageListDto.getF_PlaceOfRegistration())
				.like(!StringUtil.isEmpty(pageListDto.getF_LegalPersonName()), TBarinUser::getFLegalpersonname, pageListDto.getF_LegalPersonName())
				.eq(!StringUtil.isEmpty(pageListDto.getF_LegalPersonCertificateType()), TBarinUser::getFLegalpersoncertificatetype, pageListDto.getF_LegalPersonCertificateType())
				.like(!StringUtil.isEmpty(pageListDto.getF_LegalPersonCertificateNum()), TBarinUser::getFLegalpersoncertificatenum, pageListDto.getF_LegalPersonCertificateNum())
				.like(!StringUtil.isEmpty(pageListDto.getF_LegalPersonPhone()), TBarinUser::getFLegalpersonphone, pageListDto.getF_LegalPersonPhone())
				.like(!StringUtil.isEmpty(pageListDto.getF_LegalPersonEmail()), TBarinUser::getFLegalpersonemail, pageListDto.getF_LegalPersonEmail())
				.eq(!StringUtil.isEmpty(pageListDto.getF_UserType()), TBarinUser::getFUsertype, pageListDto.getF_UserType())
				.eq(!StringUtil.isEmpty(pageListDto.getF_SubAccountRole()), TBarinUser::getFSubaccountrole, pageListDto.getF_SubAccountRole());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addXjrBaseUser(TBarinUser tBarinUser) {
		String xjrBaseUserId = IdWorker.get32UUID();
		tBarinUser.setFUserid(xjrBaseUserId);
		//默认设置账号不启用
		tBarinUser.setFEnabledmark(0);
		//如果是服务商注册，默认用户id就是商户id
		if("1".equals(tBarinUser.getFUsertype())){
			tBarinUser.setFMerchantid(xjrBaseUserId);
		}
		boolean isSuccess = this.save(tBarinUser);
		return isSuccess ? xjrBaseUserId : null;
	}

	@Override
	public boolean updateXjrBaseUser(String id, TBarinUser TBarinUser) {
		TBarinUser.setFUserid(id);
		return this.updateById(TBarinUser);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			return true;
		}
		return false;
	}

	@Override
	public void checkUser(String account, String mobile, String userId) {
		if (StringUtil.isEmpty(account) && StringUtil.isEmpty(mobile)) {
			return;
		}
		if (this.count(Wrappers.<TBarinUser>query().lambda().ne(!StringUtil.isBlank(userId), TBarinUser::getFUserid, userId)
				.and(wrapper -> wrapper.eq(!StringUtil.isBlank(account), TBarinUser::getFAccount, account)
						.or(!StringUtil.isBlank(mobile)).eq(TBarinUser::getFMobile, mobile))) > 0) {
			throw new RuntimeException("账号或者手机号已存在！");
		}
	}

	@Override
	public TBarinUser getUserByAccountAndTelephone(String account, String telephone) {
		if(StringUtils.isNotBlank(account)&&StringUtils.isNotBlank(telephone)){
			QueryWrapper<TBarinUser> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("F_Account",account);
			queryWrapper.eq("F_Telephone",telephone);
			return this.getBaseMapper().selectOne(queryWrapper);
		}
		return null;
	}

}